#include "dspSimulator/instruction/MVC.h"

namespace dspsimulator {
    void MVC::generate_ir(const std::shared_ptr<simulator::IRGenElement> &ir_gen_element) {
        std::shared_ptr<simulator::LLVMJit> llvm_jit = ir_gen_element->m_llvm_jit;
        RegIRCopy &reg_ir_copy = ir_gen_element->m_reg_ir_copy;
        std::shared_ptr<llvm::IRBuilder<>> ir_builder = llvm_jit->ir_builder();

        auto op1 = std::dynamic_pointer_cast<SingleReg>(m_op1);
        auto op2 = std::dynamic_pointer_cast<SingleReg>(m_op2);
        std::string src_name = get_reg_name(op1->reg());
        llvm::Value *src = reg_ir_copy[src_name];
        std::string dst_name = get_reg_name(op2->reg());
        llvm::Value *dst_ptr = IRGenerator::get_global_value_ptr(llvm_jit, dst_name);
        ir_builder->CreateStore(src, dst_ptr);
    }

    std::shared_ptr<DspInstruction> MVC::decode_standard_instruction(const std::shared_ptr<DspInstructionInfo> &dsp_instruction_info) {
        std::shared_ptr<MVC> mvc;
        std::bitset<32> &data = dsp_instruction_info->m_standard_bits;
        // Operands when moving from the control file to the register
        if (common::check_bits<10>(data, 0b0011111000, 2) &&
            exist_standard_condition(common::extract_bits<4>(data, 28))) {
            mvc = std::make_shared<MVC>(dsp_instruction_info->address(), 4);
            mvc->m_creg_z = common::extract_bits<4>(data, 28);
            u32 dst = common::extract_bits<5>(data, 23).to_ulong();
            std::bitset<5> crlo = common::extract_bits<5>(data, 18);
            std::bitset<5> crhi = common::extract_bits<5>(data, 13);
            bool x = data[12];
            bool s = data[1];
            mvc->m_p = data[0];
            mvc->m_func_unit = FuncUnit::S2;
            mvc->m_op1 = std::make_shared<SingleReg>(get_creg_kind(crhi, crlo));
            mvc->m_op2 = std::make_shared<SingleReg>(get_dst_reg32_kind(dst, s));
        } else if (common::check_bits<10>(data, 0b0011101000, 2) &&
                   exist_standard_condition(common::extract_bits<4>(data, 28))) {
            // Operands when moving from the register file to the control
            mvc = std::make_shared<MVC>(dsp_instruction_info->address(), 4);
            mvc->m_creg_z = common::extract_bits<4>(data, 28);
            std::bitset<5> crlo = common::extract_bits<5>(data, 23);
            std::bitset<5> crhi = common::extract_bits<5>(data, 13);
            u32 src2 = common::extract_bits<5>(data, 18).to_ulong();
            bool x = data[12];
            bool s = data[1];
            mvc->m_func_unit = FuncUnit::S2;
            mvc->m_p = data[0];
            mvc->m_op1 = std::make_shared<SingleReg>(get_src2_reg32_kind(src2, s, x));
            mvc->m_op2 = std::make_shared<SingleReg>(get_creg_kind(crhi, crlo));
        }
        return mvc;
    }

    std::shared_ptr<DspInstruction> MVC::decode_compact_instruction(const std::shared_ptr<DspInstructionInfo> &dsp_instruction_info) {
        std::shared_ptr<MVC> mvc;
        std::bitset<16> &data = dsp_instruction_info->m_compact_bits;
        if (common::check_bits<6>(data, 0b110111, 1) && common::check_bits<6>(data, 0b110110, 10)) {
            mvc = std::make_shared<MVC>(dsp_instruction_info->address(), 2);
            mvc->m_func_unit = FuncUnit::S2;
            u32 src = common::extract_bits<3>(data, 7).to_ulong();
            bool s = true;
            mvc->m_op1 = std::make_shared<SingleReg>(get_src2_reg32_kind(src, s));
            mvc->m_op2 = std::make_shared<SingleReg>(RegKind::ILC);
            mvc->m_p = dsp_instruction_info->p();
        }
        return mvc;
    }

    std::string MVC::to_string() const {
        return get_standard_condition(m_creg_z) + m_name + " " + get_function_unit_name(m_func_unit) + " " +
               m_op1->to_string() + "," + m_op2->to_string();
    }
}